package com.springcloud.config.feign;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;

import com.springcloud.entity.UserEntity;

import feign.Param;
import feign.RequestLine;
import feign.hystrix.FallbackFactory;

//fallback = UserClientHystrixFallback.class,fallbackFactory=User2ClientHystrixFallback.class只能二选一
@FeignClient(value = "microservice-provider-user2", configuration = User2FeignConfig.class, fallback = UserClientHystrixFallback.class, fallbackFactory = User2ClientHystrixFallback.class)
public interface User2Client {

	@RequestLine("GET /get/{id}")
	UserEntity get(@Param("id") long id);

}

@Component
class User2ClientHystrixFallback implements User2Client {

	@Override
	public UserEntity get(long id) {
		UserEntity userEntity = new UserEntity();
		userEntity.setId(333l);
		return userEntity;
	}

}

@Component
class User2ClientFallbackFactory implements FallbackFactory<User2Client> {
	@Override
	public User2Client create(Throwable cause) {
		return new User2Client() {

			@Override
			public UserEntity get(long id) {
				System.err.println("fallback; reason was: " + cause.getMessage());
				return null;
			}
		};
	}
}
